# coding=utf-8

class Calculator(object):
    
    def __init__(self):
        self.action_list = []
        
    def calculate(self, obj):
        """
        Returns float that represent calculation of rating for object obj.
        It must think about operators overloading
        """
        exp = "self.calc_self(obj) "
        for operator, operand in self.action_list:
            if isinstance(operand,Calculator):
                exp += ' %s %s' % (operator, operand.calculate(obj))
            else:
                exp += ' %s %s' % (operator, operand)
        return eval(exp)
    
    def set_signals(self, model):
        """
        Set signals for auto-recount rating. Can be implemented but parent 
        set_signals must be called.
        Signals must be set
        """
        for operator, operand in self.action_list:
            if isinstance(operand, Calculator):
                operand.set_signals(model)
    
    def calc_self(self, obj):
        """
        It must be overloaded by child class.
        It calculate rating for model without thinking about operators overlaoding
        """
        raise NotImplementedError('calc_self must be implemented')
    
    def __add__(self, other):
        self.action_list.append(['+', other])
        return self
    
    def __sub__(self, other):
        self.action_list.append(['-', other])
        return self
    
    def __mul__(     self, other):
        self.action_list.append(['*', other])
        return self
    
    def __div__(self, other):
        self.action_list.append(['/', other])
        return self